package cn.exrick.xboot.modules.your.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.your.entity.BedOrder;
import cn.exrick.xboot.modules.your.service.IBedOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chenyunxuan
 */
@Slf4j
@RestController
@Api(description = "陪护床管理接口")
@RequestMapping("/xboot/bedOrder")
@Transactional
public class BedOrderController {

    @Autowired
    private IBedOrderService iBedOrderService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<BedOrder> get(@PathVariable String id) {

        BedOrder bedOrder = iBedOrderService.getById(id);
        return new ResultUtil<BedOrder>().setData(bedOrder);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<BedOrder>> getAll() {

        List<BedOrder> list = iBedOrderService.list();
        return new ResultUtil<List<BedOrder>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<BedOrder>> getByPage(@ModelAttribute PageVo page, @ModelAttribute BedOrder bedOrder) {
        QueryWrapper<BedOrder> qw = new QueryWrapper<>();
        if (null != bedOrder.getBedCode()) {

            qw.like("a.bed_code", bedOrder.getBedCode());
        }
        if (null != bedOrder.getMobile()) {

            qw.like("b.mobile", bedOrder.getMobile());
        }
        qw.eq("a.delete_flag", 0);
        if (null != bedOrder.getOrderStatus()) {
            if (bedOrder.getOrderStatus() == 4) {
                qw.apply("TIMESTAMPDIFF(HOUR,a.create_time,NOW())>24 and a.order_status=1");
            } else {
                qw.eq("order_status", bedOrder.getOrderStatus());
            }
        }
        IPage<BedOrder> data = iBedOrderService.pageOrder(PageUtil.initMpPage(page), qw);
        return new ResultUtil<IPage<BedOrder>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<BedOrder> saveOrUpdate(@ModelAttribute BedOrder bedOrder) {

        if (iBedOrderService.saveOrUpdate(bedOrder)) {
            return new ResultUtil<BedOrder>().setData(bedOrder);
        }
        return new ResultUtil<BedOrder>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids) {

        for (String id : ids) {
            iBedOrderService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}
